package com.example.yk_lx.adapter;

import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.example.yk_lx.R;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;

import java.text.SimpleDateFormat;
import java.util.List;

public class ConsAdapter extends BaseQuickAdapter<EMConversation, BaseViewHolder> {
    public ConsAdapter(int layoutResId, @Nullable List<EMConversation> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, EMConversation conversation) {
        holder.setText(R.id.tv_user,conversation.conversationId());
        TextView tvCount = holder.findView(R.id.tv_count);
        //是否显示小红点
        if(conversation.getUnreadMsgCount()==0){
            tvCount.setVisibility(View.GONE);
        }else {
            tvCount.setVisibility(View.VISIBLE);
            holder.setText(R.id.tv_count,conversation.getUnreadMsgCount()+"");
        }
        SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm");
        holder.setText(R.id.tv_time,format.format(conversation.getLastMessage().getMsgTime()));
        //
        EMMessage.Type type = conversation.getLastMessage().getType();
        if(type== EMMessage.Type.TXT){
            String message = ((EMTextMessageBody) (conversation.getLastMessage().getBody())).getMessage();
            holder.setText(R.id.tv_msg,message);
        }else{
            holder.setText(R.id.tv_msg,"其他类型的消息");
        }

    }
}
